export const getLocalStorage = (...args) => {
  const storage = {};
  args.forEach(arg => {
    storage[arg] = window.localStorage.getItem(arg) || null;
  });
  return storage;
};

export const setLocalStorage = data => {
  Object.keys(data).forEach(prop => {
    const el = data[prop];
    window.localStorage.setItem(prop, el);
  });
};
export const getUrlParams = (name) => {
  // 不传name返回所有值，否则返回对应值
  var url = window.location.search;
  if (url.indexOf("?") == 1) {
    return false;
  }
  url = url.substr(1);
  url = url.split("&");
  var name = name || "";
  var nameres;
  // 获取全部参数及其值
  for (var i = 0; i < url.length; i++) {
    var info = url[i].split("=");
    var obj = {};
    obj[info[0]] = decodeURI(info[1]);
    url[i] = obj;
  }
  // 如果传入一个参数名称，就匹配其值
  if (name) {
    for (var i = 0; i < url.length; i++) {
      for (const key in url[i]) {
        if (key == name) {
          nameres = url[i][key];
        }
      }
    }
  } else {
    nameres = url;
  }
  // 返回结果
  return nameres;
}
export const removeLocalStorage = (...args) => {
  args.forEach(arg => {
    window.localStorage.removeItem(arg);
  });
};
export const formatTime = (date, x = '/', m) => {

  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  if (m) {
    return [year, month, day].map(formatNumber).join(x)
  } else {
    return [year, month, day].map(formatNumber).join(x) + ' ' + [hour, minute, second].map(formatNumber).join(':')
  }

}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}